﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleSqlSugarTemplate
{
	public static class TestSqlSugar
	{
		private static ISqlSugarClient client;
		static TestSqlSugar()
		{
			client = new SqlSugarClient(new ConnectionConfig()
			{
				IsAutoCloseConnection = true,
				DbType=DbType.MySql,
				ConnectionString= "server=localhost;port=3306;database=test;user=root;pwd=123456;"
			});

			var ran = new Random();
			StaticConfig.CustomSnowFlakeTimeErrorFunc = () =>
			{
				return ran.Next(16, 18);//出现时间回退使用临时算法插入
			};
		}

		public static void TestInsert()
		{
			UserEntity user = new UserEntity()
			{
				CreateTime = DateTime.Now,
				Name="test"
			};
			user.Id=SnowFlakeSingle.Instance.NextId();
			client.Insertable(user).SplitTable().ExecuteCommandAsync().Wait();

			var ran = new Random();

			//Console.WriteLine(ran.Next(16, 18));

			UserEntity user1 = new UserEntity()
			{
				CreateTime = new DateTime(2026,4,1),
				Name = "test1"
			};
			user1.Id = SnowFlakeSingle.Instance.NextId();
			client.Insertable(user1).SplitTable().ExecuteCommandAsync().Wait();
		}

		//排序
		//更具表升序
		//数据倒序
		public static void TestQuery()
		{
			//var rightQuery =client.Queryable<UserEntity>().SplitTable(tabs => tabs.Take(3)).Take(10).Skip(0).ToList();

			var list = client.Queryable<UserEntity>()
			.SplitTable(tabs => tabs.Take(2).OrderBy(t=>t.Date))//近3张，也可以表达式选择
			.Take(3).Skip(3).OrderByDescending(u=>u.CreateTime).ToList();
		}


	}
}
